<?php

namespace App\Console\Commands;

use App\Game\Record;
use App\Models\Def\MainGamePlat;
use App\Models\Log\PlayerBetFlow;
use Carbon\Carbon;
use Illuminate\Console\Command;

class RecordCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'get:record {--plat=} {--startTime=} {--endTime=} {--interval=} {--delay=} {--maxPullDay=} {--firstMaxPullTime=}';
    //请求数据时间区间，单位分钟
    public $interval = 15;
    //延迟请求时间，单位分钟
    public $delay = 0;
    //最大拉取时间，单位天
    public $maxPullDay = 2;
    //首次最大拉取时间,为空时拉取当天凌晨0点开始到当前时间数据，单位分钟
    public $firstMaxPullTime = null;

    protected $now;
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->now = Carbon::now();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->interval = !empty($this->option('interval')) ? $this->option('interval') : $this->interval;
        $this->maxPullDay = !empty($this->option('maxPullDay')) ? $this->option('maxPullDay') : $this->maxPullDay;
        $this->delay = !empty($this->option('delay')) ? $this->option('delay') : $this->delay;
        $this->firstMaxPullTime = !empty($this->option('firstMaxPullTime')) ? $this->option('firstMaxPullTime') : $this->firstMaxPullTime;

        $plats = $this->getPlats();

        foreach ($plats as $plat) {
            list($startTime, $endTime) = $this->getInitTimeInterval($plat);
            $record = new Record($plat);
            $record->getBetRecord($startTime, $endTime);
        }
    }

    /**
     * gameapi
     * 2018/9/19 15:30
     * Administrator
     * getPlats
     * 获取平台类型
     * @return array
     */
    public function getPlats()
    {
        $DbPlats = MainGamePlat::where('status', 1)->get()->pluck('main_game_plat_code');

        if (empty($this->option('plat'))) {
            $plats = $DbPlats->toArray();
        } else {
            $plats = explode(',', $this->option('plat'));
            $diffPlats = collect($plats)->diff($DbPlats)->toArray();
            if (!empty($diffPlats)) {
                print_r($diffPlats);
                die("以上平台类型不存在");
            }
        }
        return $plats;
    }

    /**
     * gameapi
     * 2018/11/29 21:52
     * Administrator
     * getInitTimeInterval
     * 获取时间段
     * @param $plat
     * @return array
     */
    public function getInitTimeInterval($plat)
    {
        $start = !empty($this->option('startTime')) ? $this->option('startTime') : false;
        $end = !empty($this->option('endTime')) ? $this->option('endTime') : false;
        //时间端拉取
        if ($start && $end) {
            $startTime = Carbon::parse($start)->diffInSeconds(null, false) < 0 ? (clone $this->now) : Carbon::parse($start);
            $endTime = Carbon::parse($end)->diffInSeconds(null, false) < 0 ? (clone $this->now) : Carbon::parse($end);

            if ($startTime->diffInSeconds($endTime, false) < 0) {
                die("起始时间必须小于结束时间");
            }
        } else {
            $pullAt = $this->getPullAt($plat);
            $sysStartTime = (clone $this->now)->subMinute($this->interval + $this->delay);
            //如果系统拉取时间大于系统生成开始时间，开始间件使用系统生成时间，如果小于系统生成得开始时间则使用拉取时间
            if ($pullAt->diffInSeconds($sysStartTime, false) <= 0) {
                $startTime = (clone $this->now)->subMinute($this->interval + $this->delay);
            } else {
                $startTime = $pullAt;

            }
            //$startTime = (clone $this->now)->subMinute($this->interval + $this->delay);
            $endTime = (clone $this->now)->subMinute($this->delay);

        }

        return [$startTime, $endTime];
    }

    /**
     * gameapi
     * 2018/11/5 15:07
     * Administrator
     * getPullAt
     * 获取拉取时间
     * @param $plat
     * @param Carbon $now
     * @return Carbon
     */
    public function getPullAt($plat): Carbon
    {
        $playerBetFlow = PlayerBetFlow::where('main_game_plat_code', $plat)
            ->whereNotNull('pull_at')
            ->orderBy('pull_at', 'desc')
            ->first();
        if ($playerBetFlow) {
            $platPullAt = $playerBetFlow->pull_at;
        }

        if (isset($platPullAt)) {
            $diffDay = Carbon::parse($platPullAt)->startOfDay()->diffInDays(null, false);
            //超过
            if ($diffDay > $this->maxPullDay) {
                $time = (clone $this->now)->startOfDay();
            } else {
                $time = Carbon::parse($platPullAt);
            }
            $playerBetFlow->timestamps = false;
            $playerBetFlow->pull_at = (clone $this->now)->subMinute($this->interval + $this->delay)->toDateTimeString();
            $playerBetFlow->save();
        } else {
            if (empty($this->firstMaxPullTime)) {
                $time = (clone $this->now)->startOfDay();
            } else {
                $time = (clone $this->now)->subMinute($this->firstMaxPullTime + $this->delay);
            }
        }

        return $time;
    }

    public function __clone()
    {
        $this->now = clone $this->now;
    }
}
